// pages/DiseaseDefend/DDAdd/DDAdd.js
const db = wx.cloud.database();
const cwx = require('profunc.js');
const util = require('../../../utils/TimeUtil.js');
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    access_token:'',
    name:'',
    idcard:'',
    userloc:'',
    date:'2020-03-01',
    traffic_id:'',//车次信息
    phone:'',
    index:0,
    picker:['火车','客车','飞机'],
    region: ['山东省', '青岛市', '黄岛区'],
    _region: ['山东省', '青岛市', '黄岛区'],
  },
  lifetimes:{
    created:function(){
      //console.log('组件被创建')
     // console.log(this.data.access_token)
     
    },
    attached:function(){
      var that = this;
      db.collection('setconfig').where({
        config_name: 'access_token'
      }).get({
        success(res) {
          that.setData({
            date:util.TimeCodeYmd(),
            access_token: res.data[0].value
          })
          //console.log(res.data[0])
        },
        fail(res) {
          wx.showToast({
            title: '请求失败，无法通过扫描填充数据',
          })
        }
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    submit(){
      wx.showLoading({
        title: '上传数据',
      })
      var that = this;
      var _ = that.data;
      wx.cloud.callFunction({
        name:'clouddb',
        
        data:{
          opr:'add',
          tablename: 'authlog',
          data:{
            name:_.name,
            idcard:_.idcard,
            userloc:_.userloc,
            phone:_.phone,
            traffic_type:_.picker[_.index],
            traffic_id:_.traffic_id,
            fromprovince:_.region[0],
            fromcity:_.region[1],
            fromregion:_.region[2],
            destinprovince:_._region[0],
            destincity:_._region[1],
            destinregion:_._region[2],
            date:_.date
          }
        }
      }).then(res=>{console.log(res);wx.hideLoading();})
    },
    GetName(e){
     // console.log(e)
      this.setData({
        name:e.detail.value
      })
    },
    GetId(e){
      this.setData({
        idcard:e.detail.value
      })
    },
    GetLoc(e){
      this.setData({
        userloc:e.detail.value
      })
    },
    GetPhone(e){
      this.setData({
        phone:e.detail.value
      })
    },
    GetTraffic(e){
      this.setData({
        traffic_id:e.detail.value
      })
    },
    DateChange(e) {
      this.setData({
        date: e.detail.value
      })
    },
    ocridcard(){
      var that = this;
      cwx.OcrIdCard(that.data.access_token).then(function(_res){
        var trdata = _res.data.words_result;
        console.log(trdata)
        that.setData({
          name: trdata['姓名'].words,
          idcard: trdata['公民身份号码'].words,
          userloc: trdata['住址'].words
        })
      })      
    },
    ocrtrain_ticket(){
      var that = this;
      //识别火车票
      cwx.OcrTrain_ticket(that.data.access_token).then(function(_res){
        console.log(_res)
        that.setData({
          traffic_id:_res.data.words_result.train_num
        })
      })
    },
    RegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    RegionChange2:function(e) {
      this.setData({
        _region: e.detail.value
      })
    },
    PickerChange(e) {
      //console.log(e);
      this.setData({
        index: e.detail.value
      })
    }
  }
})
